//-----------------------------------------------------------------------
// <copyright file="DepthTarget.cs" company="Google LLC">
//
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
//-----------------------------------------------------------------------

using UnityEngine;

/// <summary>
/// Component for a depth data consuming object. The component will be automatically
/// registered to a depth source in the scene.
/// </summary>
public class DepthTarget : MonoBehaviour
{
    /// <summary>
    /// Type of depth texture to attach to the material.
    /// </summary>
    public bool UseRawDepth = false;

    /// <summary>
    /// Flag to set the depth texture as mainTexture.
    /// </summary>
    public bool SetAsMainTexture = false;

    /// <summary>
    /// Material that get a depth texture assigned.
    /// </summary>
    public Material DepthTargetMaterial;

    private void OnEnable()
    {
        // Takes the material of the object's renderer, if no DepthTargetMaterial is explicitly set.
        if (DepthTargetMaterial == null)
        {
            var renderer = GetComponent<Renderer>();

            if (renderer != null)
            {
                DepthTargetMaterial = renderer.sharedMaterial;
            }
        }

        DepthSource.SwitchToRawDepth(UseRawDepth);
        DepthSource.AddDepthTarget(this);
    }

    private void OnDisable()
    {
        DepthSource.RemoveDepthTarget(this);
    }
}
